/* Copyright (C) 2005-2013, Unigine Corp. All rights reserved.
 *
 * File:    fragment_noise_sample.h
 * Desc:    Mesh noise sample shader
 * Version: 1.04
 * Author:  Alexander Zapryagaev <frustum@unigine.com>
 *
 * This file is part of the Unigine engine (http://unigine.com/).
 *
 * Your use and or redistribution of this software in source and / or
 * binary form, with or without modification, is subject to: (i) your
 * ongoing acceptance of and compliance with the terms and conditions of
 * the Unigine License Agreement; and (ii) your inclusion of this notice
 * in any version of this software that you use or redistribute.
 * A copy of the Unigine License Agreement is available by contacting
 * Unigine Corp. at http://unigine.com/
 */

/******************************************************************************\
*
* OpenGL/OpenGLES
*
\******************************************************************************/

#ifdef OPENGL || OPENGLES
	
	fixed4 mask = saturate((texture2D(s_texture_0,s_texcoord_0.xy * mask_transform.xy + mask_transform.zw) - mask_threshold) * mask_scale);
	
	half2 texcoord_0 = s_texcoord_0.xy * noise_0_transform.xy + noise_0_transform.zw;
	half2 texcoord_1 = s_texcoord_0.xy * noise_1_transform.xy + noise_1_transform.zw;
	
	fixed4 diffuse_0 = texture2D(s_texture_1,texcoord_0);
	fixed4 diffuse_1 = texture2D(s_texture_2,texcoord_1);
	
	#ifdef MULTIPLY
		fixed4 diffuse = saturate((diffuse_0 + diffuse_1 - noise_threshold) * noise_scale) * mask;
	#elif THRESHOLD
		fixed4 diffuse = saturate((diffuse_0 + diffuse_1 - noise_threshold + mask * noise_threshold) * noise_scale);
	#else
		fixed4 diffuse = saturate((diffuse_0 + diffuse_1 - noise_threshold) * noise_scale);
	#endif
	
	#ifdef ALPHA_TEST
		if(diffuse.w <= 0.5f) discard;
	#endif
	
	fixed3 normal_0 = texture2Dxy(s_texture_3,texcoord_0).xyy;
	fixed3 normal_1 = texture2Dxy(s_texture_4,texcoord_1).xyy;
	fixed3 specular_0 = texture2D(s_texture_5,texcoord_0).xyz;
	fixed3 specular_1 = texture2D(s_texture_6,texcoord_1).xyz;
	
	#ifdef HAS_ARB_TEXTURE_SNORM
		fixed3 normal = (normal_0 + normal_1) * 0.5f;
	#else
		fixed3 normal = (normal_0 + normal_1) - 1.0f;
	#endif
	
	#ifdef MULTIPLY
		fixed3 specular = saturate((specular_0 + specular_1 - noise_threshold) * noise_scale) * mask.xyz;
	#elif THRESHOLD
		fixed3 specular = saturate((specular_0 + specular_1 - noise_threshold + mask.xyz * noise_threshold) * noise_scale);
	#else
		fixed3 specular = saturate((specular_0 + specular_1 - noise_threshold) * noise_scale);
	#endif
	
	normal.z = sqrt(saturate(1.0f - dot(normal.xy,normal.xy)));
	
	#ifdef USE_SRGB
		diffuse.xyz *= diffuse.xyz;
		specular *= specular;
	#endif
	
/******************************************************************************\
*
* Direct3D11/Direct3D119
*
\******************************************************************************/

#elif DIRECT3D11 || DIRECT3D119
	
	half4 mask = saturate((s_texture_0.Sample(s_sampler_0,IN.texcoord_0.xy * mask_transform.xy + mask_transform.zw) - mask_threshold) * mask_scale);
	
	float2 texcoord_0 = IN.texcoord_0.xy * noise_0_transform.xy + noise_0_transform.zw;
	float2 texcoord_1 = IN.texcoord_0.xy * noise_1_transform.xy + noise_1_transform.zw;
	
	half4 diffuse_0 = s_texture_1.Sample(s_sampler_1,texcoord_0);
	half4 diffuse_1 = s_texture_2.Sample(s_sampler_2,texcoord_1);
	
	#ifdef MULTIPLY
		half4 diffuse = saturate((diffuse_0 + diffuse_1 - noise_threshold) * noise_scale) * mask;
	#elif THRESHOLD
		half4 diffuse = saturate((diffuse_0 + diffuse_1 - noise_threshold + mask * noise_threshold) * noise_scale);
	#else
		half4 diffuse = saturate((diffuse_0 + diffuse_1 - noise_threshold) * noise_scale);
	#endif
	
	#ifdef ALPHA_TEST
		if(diffuse.w <= 0.5f) discard;
	#endif
	
	half3 normal_0 = s_texture_3.Sample(s_sampler_3,texcoord_0).xyz;
	half3 normal_1 = s_texture_4.Sample(s_sampler_4,texcoord_1).xyz;
	half3 specular_0 = s_texture_5.Sample(s_sampler_5,texcoord_0).xyz;
	half3 specular_1 = s_texture_6.Sample(s_sampler_6,texcoord_1).xyz;
	
	half3 normal = (normal_0 + normal_1) * 0.5f;
	
	#ifdef MULTIPLY
		half3 specular = saturate((specular_0 + specular_1 - noise_threshold) * noise_scale) * mask.xyz;
	#elif THRESHOLD
		half3 specular = saturate((specular_0 + specular_1 - noise_threshold + mask.xyz * noise_threshold) * noise_scale);
	#else
		half3 specular = saturate((specular_0 + specular_1 - noise_threshold) * noise_scale);
	#endif
	
	normal.z = sqrt(saturate(1.0f - dot(normal.xy,normal.xy)));
	
	#ifdef USE_SRGB
		diffuse.xyz *= diffuse.xyz;
		specular *= specular;
	#endif
	
#endif
